/*
 * @Author: niuxb 951259738@qq.com
 * @Date: 2022-06-13 16:55:53
 * @LastEditors: niuxb 951259738@qq.com
 * @LastEditTime: 2023-01-06 14:59:17
 * @FilePath: /syq/src/permission.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import router from './router';
import store from './store';
import {
  getToken,
  setToken
} from '@/assets/js/auth';
import util from '@/assets/js/public';
import {
  getPhone
} from '@/api/login';

const whiteList = ['/login'];
router.beforeEach((to, from, next) => {
  let gps = to.query;
  if (whiteList.indexOf(to.path) !== -1) {
    next();
  } else if (gps.accessToken) {
    console.log('accessToken', gps.accessToken)
    // if (to.name == 'newDetail' && gps.actId) {
    //   Object.assign(to.params, {
    //     actId: gps.actId,
    //     fromMoa:gps.fromMoa,
    //     title: decodeURIComponent(gps.title?gps.title:""),
    //     procedure: decodeURIComponent(gps.procedure ? gps.procedure : "")
    //   })
    // }
    if (to.name == 'newDetail' && gps.actId) {
      Object.assign(to.params, {
        actId: gps.actId,
        fromMoa:gps.fromMoa,
        title: decodeURIComponent(gps.title?gps.title:""),
        procedure: decodeURIComponent(gps.procedure ? gps.procedure : "")
      })
    }
    // if (to.name == 'wtsbdetail' && gps.id) {
    //   console.log('1111111111')
    //   Object.assign(to.params, {
    //     fromMoa:gps.fromMoa,
    //   })
    //   console.log('2222222')
    // }
    store.dispatch('SetRefreshToken', {
      refreshToken: gps.refreshToken,
      accessToken: gps.accessToken,
      userName: gps.userName
    }).then(() => {
      // setToken(gps.accessToken);
      // console.log(gps.accessToken)
      delete gps.accessToken;
      delete gps.refreshToken;
      if (to.name == 'newDetail' && gps.actId) {
        //直接进详情
        next();
      } else {
        console.log('获取用户信息')
        store.dispatch('GetInfo', {
          refreshToken: gps.refreshToken,
          accessToken: gps.accessToken,
          userName: gps.userName
        }).then(() => {
          next({
            path: to.path,
            query: gps
          });
        });
      
      }

    });
  } else if (getToken()) {
    console.log('getToken123')
    next();
  } else {
    console.log(123123123)
    next();
    // next({
    //   path: '/login'
    // });
  }
});
router.afterEach((e) => {
  console.log('跳转了')
});